package com.hanrui.android.net.presenter.message.audit.contract;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.GetNextActvityBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.audit.contract.ContractBizContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 合同
 */
public class ContractBizPresenter extends BasePresenter<ContractBizContract.View> implements ContractBizContract.Presenter{

    @Inject
    public ContractBizPresenter(){}


    @Override
    public void getNextActivity(final Context context, String taskId, String procInsId, String bizId) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .proEleContractActivity(taskId,procInsId,bizId)
                .compose(RxSchedulers.<BaseBean<GetNextActvityBean>>applySchedulers())
                .compose(mView.<BaseBean<GetNextActvityBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<GetNextActvityBean>>() {
                    @Override
                    public void accept(BaseBean<GetNextActvityBean> response) throws Exception {
                        Log.e(Constant.tag,response.getCode()+"");
                        mView.hideLoading();
                        if (response.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else if (response.getCode() == 1) {
                            mView.getNextActivity(response.getData());
                        } else {
                            mView.showFaild(String.valueOf(response.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

    @Override
    public void proEleContractBizApproval(final Context context, String nextAssigneeList, String taskId, String procInsId, String bizId, String transitionId, String comment) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .proEleContractBizApproval(nextAssigneeList,taskId,procInsId,bizId,transitionId,comment)
                .compose(RxSchedulers.<BaseBean>applySchedulers())
                .compose(mView.<BaseBean>bindToLife())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean response) throws Exception {
                        Log.e(Constant.tag,response.getCode()+"");
                        mView.hideLoading();
                        if (response.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.proEleContractBizApproval(response.getCode());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.hideLoading();
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }
}
